﻿// JScript File for handling various action for transaction document pages
function transdoc_calculateTotal(pageid, controlId, rowId, targetControl){
    //alert("in");
    var elem = document.getElementById('aspnetForm').elements;
    var totalValue = 0;
    for (var i = 0; i < elem.length; i++) {
        if (elem[i].id.indexOf("ctl00_ContentPlaceHolder3_" + pageid + "_" + controlId) > -1) {
            totalValue += transdoc_evaluvate(transdoc_getValue(elem[i]));
        }
    }
    targetControl = "ctl00_ContentPlaceHolder3_" + targetControl;
    if (document.getElementById(targetControl) != null) {
        document.getElementById(targetControl).innerHTML = totalValue.toFixed(2);
    }
}

function transdoc_evaluvate(Expr){
    try {        
        return eval(Expr);
    } 
    catch (ExEval) {
    
    }
    return 0;
}

function transdoc_format(Expr){
    try {
        return parseFloat(Expr).toFixed(2).tostring();
    } 
    catch (ExEval) {
    
    }
    return 0;
}

function transdoc_getValue(Contrl){
    if (Contrl == null) 
        return "";
    var returnValue;
    if (Contrl.type == "select-one") {
        returnValue = Contrl.options[Contrl.selectedIndex].value;
    }
    else 
        if (Contrl.type == "select-multiple") {
            returnValue = null;
        }
        else 
            if (Contrl.type == "checkbox") {
                returnValue = Boolean(Contrl.checked);
            }
            else {
                returnValue = Contrl.value;
            }
    return returnValue == "" ? 0 : returnValue;
}

function transdoc_getTotalIfTaxable(transdoc_controlIdToTotal, transdocControlIdTaxable, considerTaxableField){
    var elem = document.getElementById('aspnetForm').elements;
    var totalValue = 0;
    for (var i = 0; i < elem.length; i++) {
        if (elem[i].id.indexOf(transdoc_controlIdToTotal) > -1) {
            var transdoc_indexes = elem[i].id.split('_');
            if (considerTaxableField) {
                if (transdoc_getRealVal(transdoc_getValue(document.getElementById(transdocControlIdTaxable + "_" + transdoc_indexes[transdoc_indexes.length - 1]))) > 0) {
                    totalValue += transdoc_evaluvate(transdoc_getValue(elem[i]));
                }
            }
            else 
                totalValue += transdoc_evaluvate(transdoc_getValue(elem[i]));
        }
    }
    return totalValue;
}

function transdoc_getRealVal(transdoc_input){
    if (transdoc_input == null) 
        return 0;
    if (transdoc_input == "") 
        return 0;
    return transdoc_evaluvate(transdoc_input);
}

function transdoc_refreshTax(transdoc_hfcontrol){
    //hfcontrol is expected to have value like p_master1_grand_total,p_master1_tax_percentage|p_master1_tax_amount|p_child1_amount|p_child1_taxable,p_master1_tax_percentage1|p_master1_tax_amount1|p_child1_amount|p_child1_taxable	
    var transdoc_taxes = document.getElementById(transdoc_hfcontrol).value.split(",");
    var transdoc_commonId = "ctl00_ContentPlaceHolder3_";
    var transdoc_grandtotalControls = new Array();
    //transdoc_grandtotalControls.push(transdoc_commonId + transdoc_taxes[0]);
    transdoc_grandtotalControls.push(transdoc_taxes[0]);
    for (var j = 1; j < transdoc_taxes.length; j++) {
        if (transdoc_taxes[j].length < 1) 
            continue;
        var transdoc_eachTax = transdoc_taxes[j].split("|");
        var transdoc_percentagefield = transdoc_commonId + transdoc_eachTax[0];
        var transdoc_percentageTotal = transdoc_commonId + transdoc_eachTax[1];
        transdoc_grandtotalControls.push(transdoc_percentageTotal);
        var transdoc_percentageForAmount = transdoc_commonId + transdoc_eachTax[2];
        var transdoc_percentageTaxable = transdoc_commonId + transdoc_eachTax[3];
        if (document.getElementById(transdoc_percentagefield) != null && document.getElementById(transdoc_percentageTotal) != null) {
            {
                {
                    var transdoc_totaltaxable = transdoc_getTotalIfTaxable(transdoc_percentageForAmount, transdoc_percentageTaxable, true);
                    transdoc_totaltaxable = transdoc_totaltaxable * transdoc_getRealVal(transdoc_getValue(document.getElementById(transdoc_percentagefield))) / 100;
                    if (document.getElementById(transdoc_percentageTotal) != null) {
                        document.getElementById(transdoc_percentageTotal).value = transdoc_totaltaxable.toFixed(2);
                    }
                }
            }
        }
    }
    return transdoc_grandtotalControls;
}

function transdoc_refreshTotalFields(){
    transdoc_refreshTotalFieldsWithArg(false);
}

function transdoc_refreshTotalFieldsWithArg(calculateSubTotals){
    //alert("in");
    var transdoc_grandTotalFields = transdoc_refreshTax("ctl00_ContentPlaceHolder3_hfTransdocInfo");
    var elem = document.getElementById('aspnetForm').elements;
    var totalControls = new Array();
    var spans = document.getElementsByTagName('SPAN');
    for (var i = 0; i < spans.length; i++) {
        if (spans[i].id.length > 0) {
            if (spans[i].id.indexOf("ctl00_ContentPlaceHolder3_total_") > -1) {
                totalControls.push(spans[i].id);
            }
        }
    }
    //alert(totalControls);
    if (calculateSubTotals) {
        for (var j = 0; j < totalControls.length; j++) {
            var controlId = totalControls[j].replace("_total_", "_");
            //alert(controlId);
            for (var k = 0; k < elem.length; k++) {
                if (elem[k].id.indexOf(controlId) > -1) {
                    elem[k].onchange();
                }
            }
        }
    }
    if (transdoc_grandTotalFields != null) {
        var grandTotal = 0;
        for (var p = 1; p < transdoc_grandTotalFields.length; p++) {
            grandTotal += transdoc_evaluvate(transdoc_getValue(document.getElementById(transdoc_grandTotalFields[p])));
        }
        if (transdoc_grandTotalFields[0].length > 0) {
            var transdoc_grandTotals = transdoc_grandTotalFields[0].split("|");
            for (var j = 1; j < transdoc_grandTotals.length; j++) {
                grandTotal += transdoc_getTotalIfTaxable("ctl00_ContentPlaceHolder3_" + transdoc_grandTotals[j], "", false)
            }
            if (document.getElementById("ctl00_ContentPlaceHolder3_" + transdoc_grandTotals[0]) != null) 
                document.getElementById("ctl00_ContentPlaceHolder3_" + transdoc_grandTotals[0]).value = grandTotal.toFixed(2);
        }
    }
}
setTimeout("transdoc_refreshTotalFieldsWithArg(true)", 1000);
//window.onload=transdoc_refreshTotalFields();
